import { createSlice } from '@reduxjs/toolkit';
import axios from 'axios';
// 异步请求部分
const url = 'http://localhost:5000/api/v1_0/channel'
const channelList = [
    { id: 1, name: '推荐' },
    { id: 2, name: '热点' },
    { id: 3, name: '科技' },
    { id: 4, name: '娱乐' },
    { id: 5, name: '游戏' },
    { id: 6, name: '体育' },
    { id: 7, name: '财经' },
    { id: 8, name: '军事' }
  ];

const channelStore = createSlice({
  name: 'channel',
  initialState: {
    channelList: [],
    currentChannel: {},
  },
  reducers: {
    changeChannelList: (state, action) => {
      state.channelList = action.payload
    },
    changeCurrentChannel: (state, action) => {
      state.currentChannel = action.payload
    }
  }
})

const { changeChannelList, changeCurrentChannel } = channelStore.actions


const fetchChannelList = () => {
  return async (dispatch) => {
    const res = await axios.get(url)
    console.log('fanhuizhi', res.data.data)
    dispatch(changeChannelList(res.data.data))
    // dispatch(changeChannelList(channelList))
  }
}

// 导出action对象, 提供给页面使用 , 例如: App.js中使用
export { fetchChannelList }

export default channelStore.reducer
